package com.pasabahceambalaj.session;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.framework.EntityHome;
import org.richfaces.model.DataProvider;
import org.richfaces.model.ExtendedTableDataModel;
import org.richfaces.model.selection.Selection;
import org.richfaces.model.selection.SimpleSelection;

import com.pasabahceambalaj.entity.Firma;
import com.pasabahceambalaj.entity.Isemir;
import com.pasabahceambalaj.entity.Isemirbarkod;
import com.pasabahceambalaj.entity.Isemiretut;
import com.pasabahceambalaj.entity.Isemirhareket;
import com.pasabahceambalaj.entity.Isemirmalzeme;
import com.pasabahceambalaj.entity.Isemirurun;
import com.pasabahceambalaj.entity.Isletme;
import com.pasabahceambalaj.entity.Musteri;
import com.pasabahceambalaj.entity.Ulke;
import com.pasabahceambalaj.entity.Urun;

@Name("isemirHome")
public class IsemirHome extends EntityHome<Isemir> {

	@In(create = true)
	FirmaHome firmaHome;
	@In(create = true)
	IsletmeHome isletmeHome;
	@In(create = true)
	MusteriHome musteriHome;
	@In(create = true)
	UlkeHome ulkeHome;
	@In(create = true)
	UrunHome urunHome;

	@In(create = true)
	UrunList urunList;

	private Selection selection = new SimpleSelection();
	private String sortMode="single";
    private String selectionMode="single";
    private Object tableState;
    private ExtendedTableDataModel<Urun> dataModel;
    private List<Urun> uruns = new ArrayList<Urun>();
    private List<Urun> selectedUruns = new ArrayList<Urun>();
    
	public void setIsemirId(Long id) {
		setId(id);
	}

	public Long getIsemirId() {
		return (Long) getId();
	}

	@Override
	protected Isemir createInstance() {
		Isemir isemir = new Isemir();
		return isemir;
	}

	public void load() {
		if (isIdDefined()) {
			wire();
		}
	}

	public void wire() {
		getInstance();

		Firma firma = null;
		if (Authenticator.isAdmin()) {
			firma = firmaHome.getDefinedInstance();
		} else {
			firma = Authenticator.getFirma();
		}
		if (firma != null) {
			getInstance().setFirma(firma);
		}

		Isletme isletme = isletmeHome.getDefinedInstance();
		if (isletme != null) {
			getInstance().setIsletme(isletme);
		}
		Musteri musteri = musteriHome.getDefinedInstance();
		if (musteri != null) {
			getInstance().setMusteri(musteri);
		}
		Ulke ulke = ulkeHome.getDefinedInstance();
		if (ulke != null) {
			getInstance().setUlke(ulke);
		}
		Urun urun = urunHome.getDefinedInstance();
		if (urun != null) {
			getInstance().setUrun(urun);
		}
		
		uruns = urunList.getResultList();
	}

	public boolean isWired() {
		if (getInstance().getFirma() == null)
			return false;
//		if (getInstance().getIsletme() == null)
//			return false;
		if (getInstance().getUrun() == null)
			return false;
		return true;
	}

	public Isemir getDefinedInstance() {
		return isIdDefined() ? getInstance() : null;
	}

	public List<Isemirbarkod> getIsemirbarkods() {
		return getInstance() == null ? null : new ArrayList<Isemirbarkod>(getInstance().getIsemirbarkods());
	}

	public List<Isemiretut> getIsemiretuts() {
		return getInstance() == null ? null : new ArrayList<Isemiretut>(getInstance().getIsemiretuts());
	}

	public List<Isemirhareket> getIsemirhareketsForIsemirid() {
		return getInstance() == null ? null : new ArrayList<Isemirhareket>(getInstance().getIsemirhareketsForIsemirid());
	}

	public List<Isemirhareket> getIsemirhareketsForVirmanisemirid() {
		return getInstance() == null ? null : new ArrayList<Isemirhareket>(getInstance().getIsemirhareketsForVirmanisemirid());
	}

	public List<Isemirmalzeme> getIsemirmalzemes() {
		return getInstance() == null ? null : new ArrayList<Isemirmalzeme>(getInstance().getIsemirmalzemes());
	}

	public List<Isemirurun> getIsemiruruns() {
		return getInstance() == null ? null : new ArrayList<Isemirurun>(getInstance().getIsemiruruns());
	}

	public void takeSelection() {
		//selectedUruns.clear();
		Iterator<Object> iterator = getSelection().getKeys();
		while (iterator.hasNext()) {
			Object key = iterator.next();
			//selectedUruns.add(getUrunsDataModel().getObjectByKey(key));
			this.getInstance().setUrun(getUrunsDataModel().getObjectByKey(key));
		}
	}

	public void setSelection(Selection selection) {
		this.selection = selection;
	}

	public Selection getSelection() {
		return selection;
	}

	public ExtendedTableDataModel<Urun> getUrunsDataModel() {
        if (dataModel == null) {
            dataModel = new ExtendedTableDataModel<Urun>(new DataProvider<Urun>(){

                public Urun getItemByKey(Object key) {
                    for(Urun c : uruns){
                        if (key.equals(getKey(c))){
                            return c;
                        }
                    }
                    return null;
                }

                public List<Urun> getItemsByRange(int firstRow, int endRow) {
                    return uruns.subList(firstRow, endRow);
                }

                public Object getKey(Urun item) {
                    return item.getId();
                }

                public int getRowCount() {
                    return uruns.size();
                }
                
            });
        }
        return dataModel;
    }

	public void clearSelectedUrun(){
		this.getInstance().setUrun(null);
	}
	
	public void setSortMode(String sortMode) {
		this.sortMode = sortMode;
	}

	public String getSortMode() {
		return sortMode;
	}

	public void setSelectionMode(String selectionMode) {
		this.selectionMode = selectionMode;
	}

	public String getSelectionMode() {
		return selectionMode;
	}

	public void setTableState(Object tableState) {
		this.tableState = tableState;
	}

	public Object getTableState() {
		return tableState;
	}
	
}
